using System;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Sedna.Core.Controller;
using Sedna.Core.Controller.Configurations;
using Sedna.Core.Controls.Support;
using Sedna.Core.Controls.Support.Javascript;

namespace Sedna.Core.Controls
{

	[ValidationProperty("Company")]
	public class CompanyLocation : BaseControl, INamingContainer, IClientScriptingControl, ILabelledControl
	{
		protected LiteralControl textHeaderLiteral = new LiteralControl();
		protected Enter enter;
		protected AddressTextField addressTextField;
		
		private string textKey;

	    public new ICompanyLocationConfiguration Configuration
	    {
            get { return (ICompanyLocationConfiguration) base.Configuration; }
            set { base.Configuration = value; }
	    }
	    
		public string TextKey
		{
			get { return textKey; }
			set { textKey = value; }
		}

		public string Text
		{
			get { return textHeaderLiteral.Text; }
			set { textHeaderLiteral.Text=value;	 }
		}
		

		public override bool ClientScriptingEnabled
		{
			get { return true; }
			set { /*Always enabled*/ }
		}

		public object CompanyId
		{
			get {return enter.Value;}
		}

		public string Street1
		{
			get {return addressTextField.Street1;}
			set {addressTextField.Street1=value;}
		}

		public string Street2
		{
			get {return addressTextField.Street2;}
			set {addressTextField.Street2=value;}
		}

		public string Street3
		{
			get {return addressTextField.Street3;}
			set {addressTextField.Street3=value;}
		}


		public string City
		{
			get {return addressTextField.City;}
			set {addressTextField.City=value;}
		}

		public string State
		{
			get {return addressTextField.State;}
			set {addressTextField.State=value;}
		}

		public string Zip
		{
			get {return addressTextField.Zip;}
			set {addressTextField.Zip=value;}
		}

		public string Company
		{
			get {return enter.Text;}
			set {enter.Text=value;}
		}

        public override bool Enabled
        {
            get { return base.Enabled; }
            set
            {
                EnsureChildControls();
                base.Enabled = value;
                addressTextField.Enabled = value;
                enter.Enabled = value;
            }
        }

		public CompanyLocation(IConfiguration configuration) : base(configuration)
		{
		}

		protected override void Build()
		{
			base.Build();
			this.TextKey = configuration.TextKey;
		}

        private TableCell BuildCell(params Control[] controls)
        {
            TableCell cell = new TableCell();
            foreach (Control control in controls)
            {
                cell.Controls.Add(control);
            }
            return cell;
        }

        private TableRow BuildRow(params TableCell[] cells)
        {
            TableRow row = new TableRow();
            foreach (TableCell cell in cells)
            {
                row.Controls.Add(cell);
            }
            return row;
        }
        
	    protected override void CreateChildControls()
		{
            enter = new Enter(Configuration.EnterConfiguration);
		    addressTextField = new AddressTextField(Configuration.AddressTextFieldConfiguration);
			
		    Table table = new Table();
            table.CssClass = "innertable";
		    table.Width = Unit.Pixel(168);

            TableCell cell;
	        
			textHeaderLiteral.ID = "textHeader";
			textHeaderLiteral.TextKey = "textHeader";
			textHeaderLiteral.Text = "Company Location";
            cell = BuildCell(textHeaderLiteral);
			cell.ColumnSpan=2;
            if (HttpContext.Current.Request.Browser.Browser == "Firefox")
            {
                cell.CssClass = "innertableheaderMOZ";
            }
            else
            {
                cell.CssClass = "innertableheader";
            }
			cell.Wrap = false;

            table.Rows.Add(BuildRow(cell));

            cell = BuildCell(enter);
			cell.ColumnSpan=2;
			cell.Width=Unit.Percentage(100);

            table.Rows.Add(BuildRow(cell));

			addressTextField.ID= "addressTextField";
            cell = BuildCell(addressTextField);
			cell.ColumnSpan=2;
			cell.Width=Unit.Percentage(100);

            table.Rows.Add(BuildRow(cell));
	        
	        Controls.Add(table);
		}

		protected override void OnPreRender(EventArgs e)
		{
			base.OnPreRender(e);
			Text=Configuration.GetLocalizedPostfixLabel(TextKey, Text);
		    if (ClientScriptingEnabled)
		    {
		        DeclareScriptVariable();
		    }
		}

	    protected override void DeclareScriptVariable()
	    {
			enter.ClientScriptingEnabled=true;
			enter.DisplayControls.Add("address", addressTextField.ID);
			addressTextField.ClientScriptingEnabled=true;
	        base.DeclareScriptVariable();
	    }

	    protected override IDictionary BuildScriptParameters()
	    {
	        IDictionary parameters =  base.BuildScriptParameters();
            parameters["headerText"] = Configuration.GetLocalizedPostfixLabel(TextKey, Text);
            parameters["enter"] = enter.ClientVariable;
            parameters["address"] = addressTextField.ClientVariable;
            return parameters;
	    }

	    
		protected override void Unbind()
		{
			binder.Unbind(new string[]{"Street1", "Street2", "Street3", "City", "State", "Zip", "Company", "CompanyId", "Enabled"});
		}

		protected override void Bind()
		{
			binder.Bind(new string[]{"Company", "Street1", "Street2", "Street3", "City", "State", "Zip", "Enabled"});
		}
	}

}
